// src/utils/trie.ts
var Trie = class {
  constructor(words) {
    this.tier = 0;
    this.matched = true;
    this.wordsIndex = 0;
    this.children = {};
    words && this.build(words);
  }
  build(words) {
    const len = words.length;
    for (let i = 0; i < len; i++) {
      this.insert(words[i], 0, i);
    }
  }
  insert(word, pos = 0, wordsIndex) {
    if (word.length === 0) {
      return;
    }
    const T = this;
    let s;
    let child;
    if (pos === word.length) {
      T.wordsIndex = wordsIndex;
      return;
    }
    s = word[pos];
    if (T.children[s] === void 0) {
      T.children[s] = new Trie();
      T.matched = false;
      T.children[s].tier = this.tier + 1;
    }
    child = T.children[s];
    child.insert(word, pos + 1, wordsIndex);
  }
  searchOne(str, pos = 0) {
    if (str.length === 0)
      return null;
    const T = this;
    let child;
    let s;
    s = str[pos];
    child = T.children[s];
    if (child !== void 0 && pos < str.length) {
      return child.searchOne(str, pos + 1);
    }
    if (child === void 0 && !T.matched)
      return null;
    if (T.matched) {
      return { matchedPosition: [pos - T.tier, T.wordsIndex], tier: T.tier };
    }
    return null;
  }
  search(str) {
    if (this.matched)
      return [];
    let len = str.length;
    let searchResult = [];
    let res;
    for (let i = 0; i < len - 1; i++) {
      res = this.searchOne(str, i);
      if (res) {
        searchResult.push(res.matchedPosition);
        i = i + res.tier - 1;
      }
    }
    return searchResult;
  }
};
var trie_default = Trie;


let emojiObj = {
  "[大笑]": { file: "emoji_0.png" },
  "[可爱]": { file: "emoji_01.png" },
  "[色]": { file: "emoji_02.png" },
  "[嘘]": { file: "emoji_03.png" },
  "[亲]": { file: "emoji_04.png" },
  "[呆]": { file: "emoji_05.png" },
  "[口水]": { file: "emoji_06.png" },
  "[汗]": { file: "emoji_145.png" },
  "[呲牙]": { file: "emoji_07.png" },
  "[鬼脸]": { file: "emoji_08.png" },
  "[害羞]": { file: "emoji_09.png" },
  "[偷笑]": { file: "emoji_10.png" },
  "[调皮]": { file: "emoji_11.png" },
  "[可怜]": { file: "emoji_12.png" },
  "[敲]": { file: "emoji_13.png" },
  "[惊讶]": { file: "emoji_14.png" },
  "[流感]": { file: "emoji_15.png" },
  "[委屈]": { file: "emoji_16.png" },
  "[流泪]": { file: "emoji_17.png" },
  "[嚎哭]": { file: "emoji_18.png" },
  "[惊恐]": { file: "emoji_19.png" },
  "[怒]": { file: "emoji_20.png" },
  "[酷]": { file: "emoji_21.png" },
  "[不说]": { file: "emoji_22.png" },
  "[鄙视]": { file: "emoji_23.png" },
  "[阿弥陀佛]": { file: "emoji_24.png" },
  "[奸笑]": { file: "emoji_25.png" },
  "[睡着]": { file: "emoji_26.png" },
  "[口罩]": { file: "emoji_27.png" },
  "[努力]": { file: "emoji_28.png" },
  "[抠鼻孔]": { file: "emoji_29.png" },
  "[疑问]": { file: "emoji_30.png" },
  "[怒骂]": { file: "emoji_31.png" },
  "[晕]": { file: "emoji_32.png" },
  "[呕吐]": { file: "emoji_33.png" },
  "[拜一拜]": { file: "emoji_160.png" },
  "[惊喜]": { file: "emoji_161.png" },
  "[流汗]": { file: "emoji_162.png" },
  "[卖萌]": { file: "emoji_163.png" },
  "[默契眨眼]": { file: "emoji_164.png" },
  "[烧香拜佛]": { file: "emoji_165.png" },
  "[晚安]": { file: "emoji_166.png" },
  "[强]": { file: "emoji_34.png" },
  "[弱]": { file: "emoji_35.png" },
  "[OK]": { file: "emoji_36.png" },
  "[拳头]": { file: "emoji_37.png" },
  "[胜利]": { file: "emoji_38.png" },
  "[鼓掌]": { file: "emoji_39.png" },
  "[握手]": { file: "emoji_200.png" },
  "[发怒]": { file: "emoji_40.png" },
  "[骷髅]": { file: "emoji_41.png" },
  "[便便]": { file: "emoji_42.png" },
  "[火]": { file: "emoji_43.png" },
  "[溜]": { file: "emoji_44.png" },
  "[爱心]": { file: "emoji_45.png" },
  "[心碎]": { file: "emoji_46.png" },
  "[钟情]": { file: "emoji_47.png" },
  "[唇]": { file: "emoji_48.png" },
  "[戒指]": { file: "emoji_49.png" },
  "[钻石]": { file: "emoji_50.png" },
  "[太阳]": { file: "emoji_51.png" },
  "[有时晴]": { file: "emoji_52.png" },
  "[多云]": { file: "emoji_53.png" },
  "[雷]": { file: "emoji_54.png" },
  "[雨]": { file: "emoji_55.png" },
  "[雪花]": { file: "emoji_56.png" },
  "[爱人]": { file: "emoji_57.png" },
  "[帽子]": { file: "emoji_58.png" },
  "[皇冠]": { file: "emoji_59.png" },
  "[篮球]": { file: "emoji_60.png" },
  "[足球]": { file: "emoji_61.png" },
  "[垒球]": { file: "emoji_62.png" },
  "[网球]": { file: "emoji_63.png" },
  "[台球]": { file: "emoji_64.png" },
  "[咖啡]": { file: "emoji_65.png" },
  "[啤酒]": { file: "emoji_66.png" },
  "[干杯]": { file: "emoji_67.png" },
  "[柠檬汁]": { file: "emoji_68.png" },
  "[餐具]": { file: "emoji_69.png" },
  "[汉堡]": { file: "emoji_70.png" },
  "[鸡腿]": { file: "emoji_71.png" },
  "[面条]": { file: "emoji_72.png" },
  "[冰淇淋]": { file: "emoji_73.png" },
  "[沙冰]": { file: "emoji_74.png" },
  "[生日蛋糕]": { file: "emoji_75.png" },
  "[蛋糕]": { file: "emoji_76.png" },
  "[糖果]": { file: "emoji_77.png" },
  "[葡萄]": { file: "emoji_78.png" },
  "[西瓜]": { file: "emoji_79.png" },
  "[光碟]": { file: "emoji_80.png" },
  "[手机]": { file: "emoji_81.png" },
  "[电话]": { file: "emoji_82.png" },
  "[电视]": { file: "emoji_83.png" },
  "[声音开启]": { file: "emoji_84.png" },
  "[声音关闭]": { file: "emoji_85.png" },
  "[铃铛]": { file: "emoji_86.png" },
  "[锁头]": { file: "emoji_87.png" },
  "[放大镜]": { file: "emoji_88.png" },
  "[灯泡]": { file: "emoji_89.png" },
  "[锤头]": { file: "emoji_90.png" },
  "[烟]": { file: "emoji_91.png" },
  "[炸弹]": { file: "emoji_92.png" },
  "[枪]": { file: "emoji_93.png" },
  "[刀]": { file: "emoji_94.png" },
  "[药]": { file: "emoji_95.png" },
  "[打针]": { file: "emoji_96.png" },
  "[钱袋]": { file: "emoji_97.png" },
  "[钞票]": { file: "emoji_98.png" },
  "[银行卡]": { file: "emoji_99.png" },
  "[手柄]": { file: "emoji_100.png" },
  "[麻将]": { file: "emoji_101.png" },
  "[调色板]": { file: "emoji_102.png" },
  "[电影]": { file: "emoji_103.png" },
  "[麦克风]": { file: "emoji_104.png" },
  "[耳机]": { file: "emoji_105.png" },
  "[音乐]": { file: "emoji_106.png" },
  "[吉他]": { file: "emoji_107.png" },
  "[火箭]": { file: "emoji_108.png" },
  "[飞机]": { file: "emoji_109.png" },
  "[火车]": { file: "emoji_110.png" },
  "[公交]": { file: "emoji_111.png" },
  "[轿车]": { file: "emoji_112.png" },
  "[出租车]": { file: "emoji_113.png" },
  "[警车]": { file: "emoji_114.png" },
  "[自行车]": { file: "emoji_115.png" },
   "[囧]":'',
   "[冒汗]":'',
   "[笑哭]":'',
   "[飞吻]":'',
   "[憨笑]":'',
   "[伤心]":'',
   "[难过]":'',
   "[吐舌头]":'',
   "[斜视]":'',
   "[红心]":'',
   "[星星]":'',
   "[猪]":'',
   "[礼物]":'',
   "[红圈]":'',
   "[井]":'',
   "[兔]":'',
   "[男孩]":'',
   "[红叉]":'',
   "[牵手]":'',
   "[小鸟]":'',
   "[女孩]":'',
   "[跳舞]":'',
   "[鸡]":'',
   "[蓝瘦]":'',
   "[不]":'',
   "[鬼怪]":'',
   "[挥手]":'',
   "[狗]":'',
   "[圣诞]":'',
   "[接吻]":'',
   "[鼠]":'',
   "[外星人]":'',
   "[红唇]":'',
};

let EMOJI_DATA = [];
let nums = 0;
const yh = Math.ceil(Object.keys(emojiObj).length / 11);
for (var a = 0; a < yh; a++) {
  for (var b = 0; b < 11; b++) {
    if (nums == Object.keys(emojiObj).length) {
      break;
    }
    EMOJI_DATA.push({
      id: nums,
      cn: Object.keys(emojiObj)[nums],
      position: {
        x: b,
        y: a
      }
    });
    nums++;
  }
};

// src/config/emoji.ts
var EMOJI_PANEL = {
  // width: 400,
  // height: 1034,
  // x: 8,
  // y: 21,
  width: 724,
  x: 11,
  height: 988,
  y: yh,
  // height: 658,
  // y: 10,
  paddingTop: 0,
  paddingBottom: 0,
  paddingLeft: 0,
  paddingRight: 0,
  gapX: 0,
  gapY: 0
};


function transform2KeyPosition(data) {
  let usedKeys = ["cn", "us", "code", "web_code"];
  let res = {};
  data.forEach((item) => {
    usedKeys.forEach((key) => {
      let trieKey = item[key];
      if (trieKey && trieKey !== "[]" && !(trieKey in res)) {
        res[trieKey] = item.position;
      }
    });
  });
  return res;
}
var EMOJI_KEY_POSITION = transform2KeyPosition(EMOJI_DATA);

// src/utils/uitl.ts
function stringSplice(str, index, count, addedStr) {
  return str.slice(0, index) + addedStr + str.slice(index + count);
}

// src/parser.ts
var emojiKeys = Object.keys(EMOJI_KEY_POSITION);
var trie = new trie_default(emojiKeys);
var panelScaleCache = {};
function calculateScaleAndBgSize(panel, emojiSize) {
  if (panelScaleCache[emojiSize])
    return panelScaleCache[emojiSize];
  let { width, x, paddingLeft, paddingRight, gapX } = panel;
  let originSize = (width - paddingLeft - paddingRight - gapX * (x - 1)) / x;
  let scale = emojiSize / originSize;
  let bgSize = +(width * scale).toFixed(2);
  panelScaleCache[emojiSize] = { scale, bgSize };
  return panelScaleCache[emojiSize];
}
function getEmojiStyle(position, emojiOption) {
  let { gapX, gapY } = EMOJI_PANEL;
  const emojiSize = emojiOption.size;
  const url = emojiOption.emojiSpriteUrl;
  let { scale, bgSize } = calculateScaleAndBgSize(EMOJI_PANEL, emojiSize);
  let bgPosition = "";
  if (position) {
    let { x, y } = position;
    let left = -(x * (emojiSize + scale * gapX)).toFixed(2);
    let top = -(y * (emojiSize + scale * gapY)).toFixed(2);
    bgPosition = `${left}px ${top}px`;
  }
  return {
    display: "inline-block",
    background: `url(${url}) no-repeat`,
    width: `${emojiSize}px`,
    height: `${emojiSize}px`,
    "background-position": `${bgPosition}`,
    "background-size": `${bgSize}px`
  };
}
function transform2Html(position, emojiOption) {
  const tag = emojiOption.tag;
  let style = "";
  let styleObj = getEmojiStyle(position, emojiOption);
  if (styleObj) {
    Object.keys(styleObj).forEach((key) => {
      styleObj[key] !== void 0 && (style += `${key}: ${styleObj[key]};`);
    });
  }
  return `<${tag} style="${style}margin:0 1px 0;vertical-align:middle;"></${tag}>`;
}
let cdnUrl = 'https://rzhn-4da0b2-1305051129.file.myqcloud.com';
var defaultEmojiOption = {
  size: 64,
  tag: "a",
  emojiSpriteUrl: cdnUrl + '/emoji/emoji_h5.png'
};
function parseEmoji(str) {
  if (!str)
    return str;
  let matchedEmoji = trie.search(str);
  matchedEmoji.reverse().map(([emojiIndex, keyIndex]) => {
    let name = emojiKeys[keyIndex], position = EMOJI_KEY_POSITION[name];
    let html = transform2Html(position, defaultEmojiOption);
    str = stringSplice(str, emojiIndex, name.length, html);
  });
  return str;
}
function configParseEmoji(option) {
  if (option) {
    Object.assign(defaultEmojiOption, option);
  }
}

// src/emoji.ts
function getEmojisByEmojiData(emojiData) {
  return function (option) {
    let emojiOption = {
      size: 64,
      emojiSpriteUrl: cdnUrl + '/emoji/emoji_h5.png'
    };
    if (option) {
      emojiOption = Object.assign(emojiOption, option);
    }
    return emojiData.map(({ position, code, cn }) => {
      let style = position && getEmojiStyle(position, emojiOption) || {};
      style = JSON.stringify(style).replace(new RegExp(',', "g"), ";").replace(new RegExp('"', "g"), "").replace("{", '').replace("}", '');
      return { style, code, cn };
    });
  };
}
var getEmojis = getEmojisByEmojiData(EMOJI_DATA);

function insertStr(soure, start, newStr) {
  return soure.slice(0, start) + newStr + soure.slice(start);
}
function transformEmoji (str = '', limit) {
  if (!str) return
  let allPrevEmojiLen = 0, len = 0, emojiList = [];
  let newStr = str.replace(/\[.+?\]|<[^<>]+>/g, (ele, index) => {
      emojiList.push({
          key: ele,
          min: index - allPrevEmojiLen,
      });
      allPrevEmojiLen += ele.length - 2; // 一个表情充当2个字符
      return "";
  });
  len = newStr.length + (emojiList.length * 2); // 一个表情充当2个字符
  if (len <= limit) {
      return {
          content: str,
          overflow: false
      }
  };
  emojiList = emojiList.filter(e => e.min <= (limit - 1));
  if (newStr.length + emojiList.length > limit) {
      newStr = newStr.substr(0, (limit - emojiList.length))
  }
  let slen = 0;
  for (let item of emojiList) {
      slen += item.key.length - 2
      newStr = insertStr(newStr, item.min + slen, item.key)
  }
  return {
      content: newStr + "...",
      overflow: true
  };
}

export {
  configParseEmoji,
  getEmojis,
  parseEmoji,
  transformEmoji
};
